package formulation.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import db.nutriment.Nutriment;
import db.nutriment.NutrimentService;
import db.standard.Standard;
import db.standard.StandardService;
import db.standard.classification.Classification;
import db.standard.classification.ClassificationService;

public class StandardController extends MultiActionController
{
    private StandardService standardService;
    private ClassificationService classificationService;
    private NutrimentService nutrimentService;
    
    public void setStandardService(StandardService standardService) {
        this.standardService = standardService;
    }
    
    public void setClassificationService(ClassificationService classificationService) {
        this.classificationService = classificationService;
    }
    
    public void setNutrimentService(NutrimentService nutrimentService) {
        this.nutrimentService = nutrimentService;
    }
    
    public ModelAndView admin(HttpServletRequest request,
            HttpServletResponse response)
    {
        return new ModelAndView("standard/admin/standard", "standards", standardService.list());
    }
    
    public ModelAndView add(HttpServletRequest request,
            HttpServletResponse response, Standard standard)
    {
        standardService.insert(standard);
        return new ModelAndView("redirect:admin.do");
    }
    
    public ModelAndView remove(HttpServletRequest request,
            HttpServletResponse response)
    {
        Long id = Long.parseLong(request.getParameter("id"));
        standardService.remove(id);
        return new ModelAndView("redirect:admin.do");
    }
    
    public ModelAndView addStandard(HttpServletRequest request,
            HttpServletResponse response, Standard standard)
    {
    	Long nutrimentID = Long.parseLong(request.getParameter("nutrimentID"));
    	Long classificationID = Long.parseLong(request.getParameter("classificationID"));
    	Nutriment nutriment = nutrimentService.find(nutrimentID);
    	Classification classification = classificationService.find(classificationID);
    	Double min = Double.parseDouble(request.getParameter("min"));
    	Double max = Double.parseDouble(request.getParameter("max"));
    	standard.setName(classification.getName() + "-" + nutriment.getName());
    	standard.setClassification(classification);
    	standard.setNutriment(nutriment);
        standard.setMinInUnit(min);
        standard.setMaxInUnit(max);
    	standardService.insert(standard);
    	return new ModelAndView("redirect:/frame/standard_form.do");
    }
    
    public ModelAndView removeStandard(HttpServletRequest request,
            HttpServletResponse response, Standard standard)
    {
    	String standardID = request.getParameter("standardID");
    	standardService.remove(Long.parseLong(standardID));
    	return new ModelAndView("redirect:/frame/standard_form.do");
    }
    
    public  ModelAndView resetStandard(HttpServletRequest request,
            HttpServletResponse response, Standard standard)
    {
    	String standardID = request.getParameter("standardID");
    	Standard st = standardService.find(Long.parseLong(standardID));
    	Long nutrimentID = Long.parseLong(request.getParameter("nutrimentID"));
    	st.setNutriment( nutrimentService.find(nutrimentID));
    	Long classificationID = Long.parseLong(request.getParameter("classificationID"));
    	st.setClassification( classificationService.find(classificationID)); 	
    	st.setMax(Double.parseDouble(request.getParameter("max")));
    	st.setMin(Double.parseDouble(request.getParameter("min")));
    	st.setDescription( request.getParameter("description"));
    	standardService.insert(st);
    	return new ModelAndView("redirect:/frame/standard_form.do");
    }
    
}
